from fastapi import APIRouter
from re_common.baselibrary.tools.myparsel import MParsel
from re_common.baselibrary.utils.basedict import BaseDicts
from re_common.baselibrary.utils.core.mlamada import bools_string
from re_common.vip.baseencodeid import BaseLngid

from apps.allsubdb.paper_grrb.models import PaperGRRBParse4
from apps.core.m_route import ContextIncludedRoute
from apps.core.return_info import InputInfoModel, ReturnInfo

router = APIRouter(route_class=ContextIncludedRoute)


@router.post("/step4/parse_article_detail")
async def parse_article_one(input: InputInfoModel[PaperGRRBParse4]):
    """
    解析信息用于下载ref rel等
    :param input:
    :return:
    """
    return_info = ReturnInfo()
    jsons = input.data.jsons
    # days_url = input.data.url
    downdate = input.data.downdate
    batch = input.data.batch
    data_list = list()
    meeting_list = jsons["pages"]
    for meeting in meeting_list:
        article_list = meeting["news"]
        for article in article_list:
            rawid = article["id"].replace("grrb-", '')
            name, days, page, indexs = article["id"].split("-")
            provider_url = r"http://media.workercn.cn/sites/paper/paper_content.html?type=grrb&date={}-{}-{}&page={}&index={}".format(
                days[0:4], days[4:6], days[-2:], page, indexs)
            dic = {
                "lngid": BaseLngid().GetLngid("00379", rawid),
                "sub_db_id": "00379",
                "batch": batch,
                "rawid": rawid,
                "source_type": '11',
                "journal_name": "工人日报",
                'provider_url': provider_url,
                'title': article["title"],
                'title_catalyst': article["headline"],
                'title_alt': article["subTitle"],
                "author": article["author"],
                "abstract": article["content"].replace("<p>", '').replace("</p>", '\n'),
                'pub_year': days[0:4],
                'pub_date': days,
                'meeting_counts': page,
                'index': str(int(indexs) + 1),
                "sub_db": 'PAPER',
                "zt_provider": 'workercnpaper',
                "provider": 'WORKERCN',
                "product": 'WORKERCN',
                "country": "CN",
                "language": "ZH",
                "web_site": "http://media.workercn.cn/sites/paper/paper_index.html",
                'down_date': downdate
            }
            data_list.append(dic)
    return_info.status = bools_string(True)
    return_info.msg_code = 200
    return_info.msg = ""
    return_info.data = {"result": data_list}
    return return_info.todict()
